import store from '../store'
import {
	getWxLoginCode
} from '@/utils/login.js'
		import config from '@/config/config.js'
const http = function() {
	this.header = {
		'Content-Type': 'application/json; charset=utf-8',
	}
	this.timeout = 30000
}

http.prototype.request = function(url, method, data) {
	uni.showLoading({
		title:"加载中"
	})
	var token = uni.getStorageSync("token");
	this.header['Authorization'] = token
	var ownerId = uni.getStorageSync("ownerId");
	if(ownerId && ownerId.length > 0){
		data.ownerId = ownerId;
	}
	if(token && token.length > 0){
		// data.client="小程序";
	}
	return new Promise((resolve, rejected) => {
		console.log(`${config.baseUrl}`);
		var sendUrl = "";
		// #ifdef APP-PLUS
		sendUrl = `${config.baseUrl}` + url;
		// #endif
		// #ifdef H5
		sendUrl = url;
		// #endif
		console.log(sendUrl);
		uni.request({
			url: sendUrl,
			data: data,
			header: this.header,
			method: method,
			timeout: this.timeout,
			success: res => {
				console.log(88888888);
				const code = res.statusCode
				uni.hideLoading();
				if (code == 401) {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					//登录到期，自动登录
					// getWxLoginCode()
					return rejected(res.data.msg)
				}
				if (code == 403) {
					uni.showToast({
						title: "您没有权限这么做！",
						icon: "none"
					})
				}
				return resolve(res.data)
			},
			fail: err => {
				console.log(77777);
				uni.hideLoading();
				return rejected(err)
			}
		})
	});
}

http.prototype.get = function(url, data) {
	return this.request(url, "GET", data)
}

http.prototype.post = function(url, data) {
	return this.request(url, "POST", data)
}

http.prototype.put = function(url, data) {
	return this.request(url, "put", data)
}

http.prototype.delete = function(url, data) {
	return this.request(url, "delete", data)
}

export default new http()